//
//  MySongTableViewController.m
//  老司机发车了标准版
//
//  Created by SuperNing on 16/8/11.
//  Copyright © 2016年 斌. All rights reserved.
//

#import "MySongTableViewController.h"
#import "MusicDataBase.h"
#import "MySongTableViewCell.h"
#import "PlayerModel.h"
#import "MusicManager.h"
#import "PlayerViewController.h"

@interface MySongTableViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *dataArray;


@end

@implementation MySongTableViewController

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.backgroundColor = [UIColor brownColor];

    [self.tableView registerNib:[UINib nibWithNibName:@"MySongTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self getDataAndUpdateView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
}

- (void)getDataAndUpdateView{
    
    [[MusicDataBase shareMusicDataBase] openDB];
    self.dataArray = [[MusicDataBase shareMusicDataBase] selectOneDataWithTableName:_tableName];
    
    [self.tableView reloadData];

    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MySongTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    PlayerModel *model = self.dataArray[indexPath.row];
    cell.SongLabel.text = model.title;
    cell.SingerLabel.text = model.author;
    
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PlayerViewController *playerVC = [[PlayerViewController alloc] init];
    UINavigationController *playNC = [[UINavigationController alloc] initWithRootViewController:playerVC];
    [MusicManager shareMusicManager].index = indexPath.row;
    [MusicManager shareMusicManager].musicArray = self.dataArray;
    
    
    [self.navigationController presentViewController:playNC animated:YES completion:nil];
}





@end
